package com.example.parking.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.example.parking.config.LicensePlate;
import com.example.parking.dto.CarFrom;
import com.example.parking.dto.Result;
import com.example.parking.entity.CarInfo;
import com.example.parking.entity.User;
import com.example.parking.mapper.CarInfoMapper;
import com.example.parking.service.ICarInfoService;
import com.example.parking.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

@Slf4j
@RestController
@CrossOrigin
public class UpdateController {

    @Resource
    private IUserService userService;

    @Resource
    private ICarInfoService carInfoService;

    @Resource
    private CarInfoMapper carInfoMapper;


    /**
     * 新用户自动创建车辆信息
     * @param userId
     * @return
     */
    @GetMapping("/createCar/{userId}")
    public Result createCar(@PathVariable Integer userId){
        User user = userService.getById(userId);
        CarInfo info = carInfoMapper.getByLogin(userId);
        if (info != null){
            return Result.fail("");
        }
        //自动创建车辆信息
        CarInfo carInfo = new CarInfo();
        carInfo.setUserId(userId);
        carInfo.setPhone(user.getPhone());
        carInfo.setCreateTime(LocalDateTime.now());
        carInfo.setCarNum("");
        carInfo.setCarType("");
        carInfo.setAddress("不详");
        carInfoService.save(carInfo);
        CarFrom carFrom = BeanUtil.copyProperties(carInfo, CarFrom.class);
        return Result.successData(carFrom);
    }

    /**
     * 更新车辆信息操作
     * @param carFrom
     * @return
     */
    @PostMapping("/update")
    public Result updateUserAndCar(@RequestBody CarFrom carFrom){
        //根据carId更新信息
        CarInfo carInfo = BeanUtil.toBean(carFrom, CarInfo.class);
        if(carInfoService.updateById(carInfo)){
            return Result.successData("更新成功");
        }else{
            return Result.fail("更新失败");
        }
    }

    /**
     * 获取当前的车辆信息
     * @param carId
     * @return
     */
    @GetMapping("/getNowCar/{carId}")
    public Result getNowCar(@PathVariable Integer carId){
        CarInfo carInfo = carInfoService.getById(carId);
        CarFrom carFrom = BeanUtil.copyProperties(carInfo, CarFrom.class);
        return Result.successData(carFrom);
    }

    /**
     * 识别上传的车牌图片
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile multipartFile){
        //获取属性
        String fileName = multipartFile.getOriginalFilename();
        //上传图片存放在某个文件夹路径下——真实情况是存储在真实服务器中
        String path = "E:\\Parking\\images\\";

        File file = new File(path + fileName);
        String filePath = path + fileName;
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("文件上传失败");
        }
        //调用识别方法
        JSON data = JSONUtil.parse(LicensePlate.licensePlate(filePath));
        return Result.successData(data);
    }
}